import Vue from 'vue'//引入Vue.js
import Vuex from 'vuex'//引入Vuex
Vue.use(Vuex)//使用Vuex
export default new Vuex.Store({
  state: {
    list : localStorage.getItem('list')?JSON.parse(localStorage.getItem('list')):[{
      img : require("@/assets/images/oppo.jpg"),
      name : "OPPO R15 智能手机 全网通",
      num : 2,
      price : 599
    },{
      img : require("@/assets/images/vivo.jpg"),
      name : "vivo X27 8GB+256GB大内存",
      num : 1,
      price : 699
    }]
  },
  mutations: {
    add: function (state, newShop) {
      state.list.push(newShop);//添加到商品数组
      localStorage.setItem('list',JSON.stringify(state.list));//存储商品列表
    },
    del: function (state, index) {
      state.list.splice(index, 1);//删除商品
      localStorage.setItem('list',JSON.stringify(state.list));//存储商品列表
    }
  },
  actions: {
    addAction: function (context,newShop) {
      context.commit('add', newShop);//提交mutation并传递参数
    },
    delAction: function (context,index) {
      context.commit('del', index);//提交mutation并传递参数
    }
  }
})
